package real.bytedance.window;

import org.junit.Test;

import utils.ArrayGenerator;

public class LongestUnrepeatSubstring {
    //剑指48
    class Solution {
        public int lengthOfLongestSubstring(String s) {
            int len;
            if (s == null || (len = s.length()) == 0) return 0;
            int hi = 0, lo = 0, res = 0, l, start = 0;
            int[] dict = new int[128];
            char c;
            while (hi < len) {
                while (hi < len) {
                    dict[(c = s.charAt(hi))]++;
                    if (dict[c] > 1) break;
                    hi++;
                }
                if ((l = hi - lo) > res) {
                    res = l;
                    start = lo;
                }
                if (hi >= len) break;
                while (dict[s.charAt(hi)] > 1) {
                    dict[s.charAt(lo)]--;
                    lo++;
                }
                hi++;
            }
            System.out.println(s.substring(start, start + res));
            return res;
        }
    }


    @Test
    public void test() {
        Solution s = new Solution();
        s.lengthOfLongestSubstring("fhvyvyaorqrquzv");
        System.out.println(s.lengthOfLongestSubstring("  "));
        System.out.println(s.lengthOfLongestSubstring("_8"));
        // for (int i = 0; i < 50_0000; i++) {
        //     String str = ArrayGenerator.getString();
        //    try{
        //         s.lengthOfLongestSubstring(str);
        //    } catch(Exception e) {
        //        System.out.println(str);
        //    }
        // }
    }
}
